//
//  WelcomeViewController.swift
//  MicroBlog
//
//  Created by Apple on 16/9/22.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import SDWebImage
//欢迎页面控制器
class WelcomeViewController: UIViewController {
    //MARK: --懒加载
    //背景图
    private lazy var bgImageV:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //头像
    private lazy var userImageV:UIImageView = {
       let view = UIImageView(image: UIImage(named: "avatar_default_big"))
        if let imageUrl = HMUserAccountViewModel.sharedUserAccountViewModel.userAccount?.avatar_hd{
            //url不为空
            view.sd_setImage(with: URL(string:imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        }
        view.layer.cornerRadius = view.width / 2
        view.layer.masksToBounds = true
        return view
    }()
    //文本信息
    private lazy var messageLabel: UILabel = {
       let label = UILabel()
        if let name = HMUserAccountViewModel.sharedUserAccountViewModel.userAccount?.name {
            label.text = "欢迎回来,\(name)"
        } else {
            label.text = "欢迎回来"
        }
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        label.alpha = 0
        return label
    }()
    //重写LoadView
    override func loadView() {
        view = bgImageV
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    //添加控件设置约束
    private func setupUI() {
        view.addSubview(userImageV)
        view.addSubview(messageLabel)
        
        
        userImageV.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(userImageV)
            make.top.equalTo(userImageV.snp_bottom).offset(10)
        }
    }
    //重写viewDidAppear
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        startAnimation()
    }
    
    //开启动画
    private func startAnimation() {
        userImageV.snp_updateConstraints { (make) in
            make.top.equalTo(view).offset(100)
        }
        //执行约束动画
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: {
            self.view.layoutIfNeeded()
            }) { (_) in
                UIView.animate(withDuration: 1, animations: { 
                    self.messageLabel.alpha = 1
                    }, completion: { (_) in
                        //跳转到首页
                        NotificationCenter.default.post(name: Notification.Name(SwitchRootVCNotification), object: nil)
                })
        }
    }

}
